package com.ushine.web.component.scheduled.job;

import com.ushine.web.component.scheduled.config.QuartzJobQuartzConfig;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Author: yls
 * @Date 2022/2/18 14:47
 * @Description: 配置文件
 * @Version 1.0
 **/
@Configuration
public class GeneralQuartzConfig {

    /**
     * 任务名称
     */
    private static final String JOB_NAME = "generalJob";

    /**
     * 任务所属组名称
     */
    private static final String JOB_GROUP_NAME = "generalJob";

    /**
     * 定时任务描述信息
     */
    private static final String DESCRIPTION = "模板定时器测试";

    @Resource
    private QuartzJobQuartzConfig quartzJobQuartzConfig;

    /**
     * 功能描述:
     * 〈使用jobDetail包装job
     * 必须指定Bean的名称，格式为： 类名(开头小写)+Detail，不可重复
     * 〉
     *
     * @return : org.quartz.JobDetail
     * @author : yls
     * @date : 2022/2/18 14:48
     */
    @Bean("generalDetail")
    public JobDetail generalDetail() {
        Class<GeneralJob> generalJobClass = GeneralJob.class;
        return quartzJobQuartzConfig.getJobDetail(generalJobClass, JOB_NAME, JOB_GROUP_NAME);
    }

    /**
     * 功能描述:
     * 〈把jobDetail注册到Cron表达式的trigger上去，
     * 必须指定Bean的名称，格式为： 类名(开头小写)+Trigger，不可重复
     * 〉
     *
     * @param
     * @return : org.quartz.Trigger
     * @author : yls
     * @date : 2022/2/18 14:48
     */
    @Bean("generalTrigger")
    public Trigger generalTrigger() {
        JobDetail jobDetail = generalDetail();
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInHours(12).repeatForever();
        return quartzJobQuartzConfig.getCronTrigger(jobDetail, scheduleBuilder,
                JOB_NAME, JOB_GROUP_NAME, DESCRIPTION);
    }

}
